﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditCollectionItem : Form
    {
        private SuperTalkCollections superTalkCollections;

        public FormEditCollectionItem(string referSuperTalk)
        {
            InitializeComponent();
            superTalkCollections = new SuperTalkCollections(referSuperTalk);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void FormEditCollectionItem_Load(object sender, EventArgs e)
        {
            comboBoxKind.Items.Clear();
            foreach (var n in Collection.List.Values)
            {
                comboBoxKind.Items.Add(n);
            }
            if (Collection.List.Values.Count > 0) comboBoxKind.SelectedIndex = 0;
        }

        private void comboBoxGroups_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            e.DrawBackground();

            string s = "";
            if ((this.comboBoxGroups.Items[e.Index] as string) == "")
            {
                s = "(No Group)";
            }
            else if (this.comboBoxGroups.Items[e.Index] is SuperTalkCollections.CG)
            {
                var item = (SuperTalkCollections.CG)this.comboBoxGroups.Items[e.Index];
                s = item.Name + " (" + item.Id + ")";
            }
            else
            {
                var item = (CollectionItem)this.comboBoxGroups.Items[e.Index];
                s = item.Name + " (" + item.Id + ")";
            }
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        private void comboBoxKind_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            e.DrawBackground();
            var item = (Collection)this.comboBoxKind.Items[e.Index];
            string s = item.HumanReadableName + " (" + item.Id + ")";
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        public Collection GetSelectedCollection()
        {
            if (comboBoxKind.SelectedIndex < 0) return null;
            return (Collection)this.comboBoxKind.Items[comboBoxKind.SelectedIndex];
        }

        private void comboBoxKind_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxGroups.Items.Clear();
            var kind = GetSelectedCollection();
            if (kind == null) return;
            comboBoxGroups.Items.Add("");   // no group
            comboBoxGroups.SelectedIndex = 0;
            foreach (var n in superTalkCollections.CollectionGroups.Values)
            {
                comboBoxGroups.Items.Add(n);
            }
            foreach (var n in kind.Collections)
            {
                // 子階層を持っていないなら対象にしない
                // 子階層が0個であってもここでリストされねばならない
                if (n.GetRawSubItems == null) continue;
                comboBoxGroups.Items.Add(n);
            }
        }

        internal string GetResultText()
        {
            if (comboBoxKind.SelectedIndex < 0) return "";
            if (comboBoxGroups.SelectedIndex < 0) return "";
            if (string.IsNullOrWhiteSpace(this.textBoxName.Text)) return "";
            var kind = (Collection)this.comboBoxKind.Items[comboBoxKind.SelectedIndex];
            var group = this.comboBoxGroups.Items[comboBoxGroups.SelectedIndex];
            if ((group as string) == "")
            {
                return "#C " + kind.Id + " \"" + textBoxName.Text + "\" " + Guid.NewGuid().ToString() + "\r\n";
            }
            else
            {
                string groupId = "";
                if (group is SuperTalkCollections.CG)
                {
                    groupId = ((SuperTalkCollections.CG)group).Id;
                }
                else
                {
                    groupId = ((CollectionItem)group).Id;
                }
                return "#C " + kind.Id + " \"" + textBoxName.Text + "\" " + groupId + " " + Guid.NewGuid().ToString() + "\r\n";
            }
        }
    }
}
